﻿// Decompiled with JetBrains decompiler
// Type: System.Web.Razor.Text.SeekableTextReader
// Assembly: System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: 682513D6-F37E-48AC-9909-F3EE103CFF14
// Assembly location: D:\Project\Idea\无人机外网\uav\客户端工程\排查工具\bin\Release\System.Web.Razor.dll

using System.IO;

namespace System.Web.Razor.Text
{
  /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Represents a reader </summary>
  public class SeekableTextReader : TextReader, ITextDocument, ITextBuffer
  {
    private int _position;
    private LineTrackingStringBuffer _buffer = new LineTrackingStringBuffer();
    private SourceLocation _location = SourceLocation.Zero;
    private char? _current;

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Initializes a new instance of the <see cref="T:System.Web.Razor.Text.SeekableTextReader" /> class.</summary>
    /// <param name="content">The string content.</param>
    public SeekableTextReader(string content)
    {
      this._buffer.Append(content);
      this.UpdateState();
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Initializes a new instance of the <see cref="T:System.Web.Razor.Text.SeekableTextReader" /> class.</summary>
    /// <param name="source">The source reader.</param>
    public SeekableTextReader(TextReader source)
      : this(source.ReadToEnd())
    {
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Initializes a new instance of the <see cref="T:System.Web.Razor.Text.SeekableTextReader" /> class.</summary>
    /// <param name="buffer">The text buffering.</param>
    public SeekableTextReader(ITextBuffer buffer)
      : this(buffer.ReadToEnd())
    {
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets the source of location for the text reader.</summary>
    /// <returns>The source of location for the text reader.</returns>
    public SourceLocation Location => this._location;

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets the length of the text to read.</summary>
    /// <returns>The length of the text to read.</returns>
    public int Length => this._buffer.Length;

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Gets or sets the position to seek the text reader.</summary>
    /// <returns>The position to seek the text reader.</returns>
    public int Position
    {
      get => this._position;
      set
      {
        if (this._position == value)
          return;
        this._position = value;
        this.UpdateState();
      }
    }

    internal LineTrackingStringBuffer Buffer => this._buffer;

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Reads the next character from the text reader and advances the character position by one character.</summary>
    /// <returns>The next character from the text reader, or -1 if no more characters are available.</returns>
    public override int Read()
    {
      if (!this._current.HasValue)
        return -1;
      int num = (int) this._current.Value;
      ++this._position;
      this.UpdateState();
      return num;
    }

    /// <summary>This type/member supports the .NET Framework infrastructure and is not intended to be used directly from your code.Reads the next character without changing the state of the reader or the character source.</summary>
    /// <returns>An integer representing the next character to be read.</returns>
    public override int Peek() => !this._current.HasValue ? -1 : (int) this._current.Value;

    private void UpdateState()
    {
      if (this._position < this._buffer.Length)
      {
        LineTrackingStringBuffer.CharacterReference characterReference = this._buffer.CharAt(this._position);
        this._current = new char?(characterReference.Character);
        this._location = characterReference.Location;
      }
      else if (this._buffer.Length == 0)
      {
        this._current = new char?();
        this._location = SourceLocation.Zero;
      }
      else
      {
        this._current = new char?();
        this._location = this._buffer.EndLocation;
      }
    }
  }
}
